<?php

namespace app\user\controller;

use think\Request;
use think\Db;

class User {
  public function register () {
    // 获取实例
    $request = Request::instance();
    var_dump($request->post());
    $data = $request->post();
    var_dump($data);
    // // 设置了前缀
    $result = Db::name('user')->insert($data);
    echo $result;
    var_dump($result);
    if ($result) {
      return json('注册成功');
    } else {
      return json('注册失败');
    }
    // // 没有设置前缀
    // Db::table('php_user')->insert($data); // 添加数据要数组
  }
  public function login () {
    $request = Request::instance();
    $data = $request->post();
    var_dump($request->post());
    $email = $data['email'];
    $password = $data['password'];
    // 查询数据
    $result = Db::table('php_user')
    ->where('email', $email)
    ->where('password', $password)
    ->find();
    echo '-----result-----';
    var_dump($result);
    if ($result) {
      return json('登录成功');
    } else {
      return json('登录失败');
    }
  }
  public function updateUser () {
    $request = Request::instance();
    $data = $request->post();
    $nickname = $data['nickname'];
    $email = $data['email'];
    $password = $data['password'];
    var_dump($data);
    $result = Db::table('php_user')
    ->where('email', $email)
    ->update([
      'password' => $password,
      'nickname' => $nickname
    ]);
    // ->update(['password' => $password, 'nickname' => $nickname]);
    // ->update($data); // 整体覆盖
    echo '-----result------';
    var_dump($result);
    if ($result) {
      return json('更新成功');
    } else {
      return json('更新失败');
    }
  }
  public function deleteUser () {
    $request = Request::instance();
    $data = $request->get();
    echo $data['id'];
    var_dump($data['id']);
    $id = $data['id'];
    $result = Db::table('php_user')
    ->where('id', $id)
    ->delete();
    echo $result;
    var_dump($result);
    if ($result === 1) {
      return json('删除成功');
    } else {
      return json('删除失败');
    }
  }
  public function test () {
    $request = Request::instance();

    $data = $request->post();
    $email = $data['email'];
    $result = Db::table('php_user')
    ->where('email', '<>',  $email)
    ->select(); // 查询一堆用这个
    var_dump($result);
  }
}